﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class sysadmin_CourseManager_ResourceComment : PowerCreator.LMS.Web.Base.BasePage
{
    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
    PowerCreator.LMS.Web.Grade.GradeManager objGradeManager = new PowerCreator.LMS.Web.Grade.GradeManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            ViewState["ResourceID"] = "0";
            if (Request.QueryString["ResourceID"] != null)
            {
                ViewState["ResourceID"] = Request.QueryString["ResourceID"].ToString();
            }
            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }
    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("ResourceComment_lbl_title", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_UserName.Text = GetLanageValueByKey("lbl_EditStudent_LoadingName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_RealName.Text = GetLanageValueByKey("lbl_EditStudent_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Title.Text = GetLanageValueByKey("ResourceComment_gv_c1", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Search.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        chkAll.Text = GetLanageValueByKey("RoleUser_rdbtn_YesNoAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("SetBaseAction_rdbtn_All", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnDel.Text = GetLanageValueByKey("btn_Course_btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnDel.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        base.InitPage();
    }
    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append(string.Format(" FKGradeResourceID = {0} and (GradeContent is not null) and FKGradeSkillID=0 ", ViewState["ResourceID"]));
        //if (!string.IsNullOrEmpty(txt_UserName.Text))
        //{
        //    sbSearch.Append(string.Format(" and CreatedUserName like '%{0}%'", txt_UserName.Text));
        //}
        //if (!string.IsNullOrEmpty(txt_RealName.Text))
        //{
        //    sbSearch.Append(string.Format(" and CreatedRealName like '%{0}%'", txt_RealName.Text));
        //}
        if (!string.IsNullOrEmpty(txt_Title.Text))
        {
            sbSearch.Append(string.Format(" and GradeContent like '%{0}%'", txt_Title.Text));
        }
        return sbSearch.ToString();
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        }
    }

    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objGradeManager.CountGrade(search);
        AspNetPager1.PageSize = LMSPageSize;
    }

    private void BinderRPT(string search, int pageIndex)
    {
        string orderList = "";
        DataTable dt = objGradeManager.QueryGrade(search, orderList, PowerCreator.LMS.Web.Config.WebConfig.PageSize, pageIndex);

        dt.Columns.Add("GradeContentShort");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["GradeContentShort"] = PowerCreator.LMS.Common.Utils.StringUtils.Left(dt.Rows[i]["GradeContent"].ToString(), 12);
        }
        rpt_Comment.DataSource = dt;
        rpt_Comment.DataBind();
        PreAspNetPager(AspNetPager1);
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rpt_Comment_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
       
        if (e.CommandName == "ldel")
        {
            if (objGradeManager.RemoveGrade(int.Parse(e.CommandArgument.ToString())))
            {
                string search = GetSearch();
                BinderPager(search);
                BinderRPT(search, AspNetPager1.CurrentPageIndex);
            }
            else
            {
                Label lbl_rpt_msg = (Label)e.Item.FindControl("lbl_rpt_msg");
                //lbl_rpt_msg.Text = "删除数据出现异常!请联系管理员!";
                lbl_rpt_msg.Text = GetLanageValueByKey("ResourceComment_msg_m2", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
        }
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选/取消
        for (int i = 0; i < rpt_Comment.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_Comment.Items[i].FindControl("chkSelect");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rpt_Comment.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_Comment.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_del_Click(object sender, EventArgs e)
    {
        //删除选中
        for (int i = 0; i < rpt_Comment.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_Comment.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lblid = (Label)rpt_Comment.Items[i].FindControl("lblid");
                objGradeManager.RemoveGrade(int.Parse(lblid.Text.ToString()));
            }
        }
        chkAll.Checked = false;
        chkCancel.Checked = false;
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
}
